<script setup>
import { ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { useEnterpriseStore } from '@/stores/index'
import { useRouter } from 'vue-router'
import {
  enterpriseLoginService,
  enterpriseRegisterService
} from '@/api/enterprise.js'
const form = ref()
const isRegister = ref(false)
const ruleForm = ref({
  username: '',
  password: '',
  repassword: ''
})

/*规则*/
const rules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名必须是 5-10位的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { pattern: /^\S{6,15}$/, message: '密码必须是6-15位的非空字符' }
  ],
  repassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15的非空字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        if (value !== ruleForm.value.password) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
})

/*登录相关*/
const router = useRouter()
const enterpriseStore = useEnterpriseStore()
const login = async () => {
  await form.value.validate() // 登录前检验
  const res = await enterpriseLoginService({
    username: ruleForm.value.username,
    password: ruleForm.value.password
  })
  enterpriseStore.setToken(res.data.data)
  enterpriseStore.setEnterpriseUsername(ruleForm.value.username)
  ElMessage.success('登录成功')
  router.push(`/enterprise`)
}

/*注册相关*/
const register = async () => {
  await form.value.validate() // 注册前检验
  await enterpriseRegisterService({
    username: ruleForm.value.username,
    password: ruleForm.value.password
  })
  ElMessage.success('注册成功')
}
</script>

<template>
  <el-row class="container">
    <el-col :span="12" :offset="6" class="form">
      <h2 class="title">Enterprise</h2>
      <!-- 登录表单 -->
      <el-form
        ref="form"
        :model="ruleForm"
        :rules="rules"
        size="large"
        autocomplete="off"
        v-if="isRegister === false"
      >
        <el-form-item prop="username">
          <el-input
            class="input"
            v-model="ruleForm.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            show-password="ture"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <div class="flex">
            <el-checkbox><span class="tips">记住我</span></el-checkbox>
            <el-link
              href="https://www.bilibili.com/"
              target="_blank"
              :underline="false"
              ><span class="tips">忘记密码？</span></el-link
            >
          </div>
        </el-form-item>
        <el-form-item>
          <el-button @click="login" class="button" round auto-insert-space
            >登录</el-button
          >
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = true">
            注册 →
          </el-link>
        </el-form-item>
      </el-form>
      <!-- 注册表单 -->
      <el-form
        ref="form"
        :model="ruleForm"
        :rules="rules"
        size="large"
        autocomplete="off"
        v-else
      >
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            show-password="ture"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="ruleForm.repassword"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入再次密码"
            show-password="ture"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="register()" round class="button" auto-insert-space>
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = false">
            ← 登录
          </el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
@import '@/assets/styles/loginPage.css';
</style>
